﻿package com.msjt.dao;

import java.util.List;

import com.msjt.base.BaseDAO;
import com.msjt.entity.Users;
/**
 * name:UsersDAO
 * function:用户模块
 * author: kw
 * time:2013.8.27
 * updateTime:
 */
public class UsersDAO extends BaseDAO {
	//得到用户数量
	public Integer getUsersCount(){
		String hql="select count(u.usersId) from Users as u";
		return this.getTotalCount(hql, null);
	}
	//用户登录
	@SuppressWarnings({ "unchecked" })
	public Users login(Users users){
		String hql="select u from Users as u where u.usersName=? and u.usersPwd=?";
		Object [] params={users.getUsersName(),users.getUsersPwd()};
		List<Users> userList=(List<Users>) this.searchByHQL(hql, params);
		if(userList.size()==0||userList==null){
			return null;
		}	
		return (Users) this.searchByHQL(hql, params).get(0);
	}
	//得到所有用户信息分页显示
	@SuppressWarnings("unchecked")
	public List<Users> getUsersPage(int pageIndex,int pageSize){
		String hql="select u from Users as u order by u.usersId";
		return (List<Users>) this.searchByPage(hql, null, pageIndex, pageSize);
	}
	//根据ID得到用户信息
	public Users getUsersInfo(Users users){
		return (Users) this.findById(users.getUsersId());
	}
	//用户信息修改
	public boolean updateUserInfo(Users users){
		return this.saveOrUpdate(users);
	}
	//添加OA管理用户
	public boolean addUsers(Users users){
		return this.save(users);
	}
	//删除用户
	public boolean deleteUsers(Users users){
		return this.deleteById(users.getUsersId());
	}
	//修改密码
	public boolean updatePwd(Users users){
		String hql="update Users as u set u.usersPwd=? where u.usersId=?";
		Object [] params={users.getUsersPwd(),users.getUsersId()};
		return this.updateByHql(hql, params);
	}
	//根据帐号查找用户是否存在
	public boolean findUsersName(Users users){
		String hql="select u from Users as u where u.usersName=?";
		Object [] params={users.getUsersName()};
		@SuppressWarnings("unchecked")
		List<Users> usersList=(List<Users>) this.searchByHQL(hql, params);
		if(usersList==null||usersList.size()<=0){
			return true;
		}
		return false;
	}
	//模糊查询用户,得到用户数量
		public Integer getUsersCount(Users users){
			if((!users.getUsersName().equals(""))&&users.getRealName()!=null&&users.getRealName().equals("")||users.getRealName()==null){
				String hql="select count(u.usersId) from Users as u where u.usersName like ?";
				Object [] params={"%"+users.getUsersName()+"%"};
				return this.getTotalCount(hql, params);
			}
			if((!users.getRealName().equals(""))&&users.getRealName()!=null&&users.getUsersName().equals("")||users.getUsersName()==null){
				String hql="select count(u.usersId) from Users as u where u.realName like ?";
				Object [] params={"%"+users.getRealName()+"%"};
				return this.getTotalCount(hql, params);
			}
			//用户名和真实姓名同时筛选
			String hql="select count(u.usersId) from Users as u where u.usersName like ? and u.realName like ?";
			Object [] params={"%"+users.getUsersName()+"%","%"+users.getRealName()+"%"};
			return this.getTotalCount(hql, params);
			
		}
	//模糊查询用户
	@SuppressWarnings("unchecked")
	public List<Users> selectUsersLike(Users users,int pageIndex,int pageSize){
		//根据用户名查询
		if((!users.getUsersName().equals(""))&&users.getRealName()!=null&&users.getRealName().equals("")||users.getRealName()==null){
			String hql="select u from Users as u where u.usersName like ?";
			Object [] params={"%"+users.getUsersName()+"%"};
			return (List<Users>) this.searchByPage(hql, params, pageIndex, pageSize);
		}
		//根据真实姓名筛选
		if((!users.getRealName().equals(""))&&users.getRealName()!=null&&users.getUsersName().equals("")||users.getUsersName()==null){
			String hql="select u from Users as u where u.realName like ?";
			Object [] params={"%"+users.getRealName()+"%"};
			return (List<Users>) this.searchByPage(hql, params, pageIndex, pageSize);
		}
		//用户名和真实姓名同时筛选
		String hql="select u from Users as u where u.usersName like ? and u.realName like ?";
		Object [] params={"%"+users.getUsersName()+"%","%"+users.getRealName()+"%"};
		return (List<Users>) this.searchByPage(hql, params, pageIndex, pageSize);
	}
	
	/**
	 * 批量删除
	 * @param idString id的字符串，用“,”号隔开
	 * @return
	 */
	public boolean deleteMoreUsers(String idString){
		String hql="delete Users as u where u.usersId in ("+idString+")";
	//	Object [] param={idString};
		return this.updateByHql(hql, null);
	}
}
